<template>
  <div class="topNav">
    <van-popup v-model:show="this.$store.state.menuShow" position="left" :style="{ height: '100%', width: '70%' }">
      <Menu></Menu>
    </van-popup>
    <div class="topLeft" @click="this.$store.commit('updateMenuShow')">
      <svg class="icon" aria-hidden="true">
        <use xlink:href="#icon-caidan"></use>
      </svg>
    </div>

    <div class="topContent">
      <span v-for="(nav, index) in state.navList" :key="index" @click="changeActive(index)"
        :class="{ active: index == state.currentIndex }">{{ nav }}</span>
    </div>

    <div class="topRight">
      <svg class="icon" aria-hidden="true" @click="$router.push('/search')">
        <use xlink:href="#icon-sousuo"></use>
      </svg>
    </div>
  </div>


</template>

<script>
import { reactive } from 'vue'
import { useRoute, useRouter } from 'vue-router'
import Menu from '@/components/Home/Menu.vue'
export default {
  components: {
    Menu
  },
  setup() {
    const state = reactive({
      // 导航栏选项(用于for循环)
      navList: ['Mine', 'Discover', 'Moment', 'Video'],
      // 用来判定导航栏选项当前的index值，
      currentIndex: 1
    })

    const route = useRoute()
    const router = useRouter()

    // 点击事件：改变当前活动nav颜色提示
    function changeActive(index) {
      state.currentIndex = index
      // 如果点击的是mine就跳转到对应的路由
      if (state.currentIndex == 0) {
        router.push('/userinfo')
      }
      // 如果点击的是mv，就跳转到mv路由
      if (state.currentIndex == 3) {
        router.push('/mv')
      }
      // 如果点击的是Discover就跳转到发现路由
      if (state.currentIndex == 1) {
        router.push('/')
      }
      // 如果点击的是moment就跳转到moment路由
      if (state.currentIndex == 2) {
        router.push('/moment')
      }
    }

    return { state, changeActive }
  }

}
</script>

<style lang="less" scoped>
.topNav {
  width: 100%;
  height: 1rem;
  padding: .2rem;
  display: flex;
  justify-content: space-between;
  align-items: center;
  background-color: white;

  .topContent {
    flex: 1;
    font-size: 16px;
    display: flex;
    justify-content: center;
    font: bold 16px "Courier";

    span {
      margin: 0 .12rem;
    }

    .active {
      border-bottom: .04rem solid rgb(52, 215, 155);
      color: rgb(3, 139, 89);
    }
  }
}
</style>